<?php

namespace app\common\redis\db0;

/**
 * userinfo
 */
class Weixin extends RedisAbstract {

    /**
     * 表名
     * 
     * @var string
     */
    protected $_tableName = '';

    /**
     * 计算key
     */
    protected function _calcKey($id) {
        return $this->_tableName . $id;
    }

    /**
     * @param int $id 主键
     * 
     * @return array
     */
    public function get($id) {
        if (!$this->_getEnable()) {
            return null;
        }
        $data = $this->getRedis()->get($this->_calcKey($id));
        return json_decode($data, true);
    }

   
    /**
     * @param int $id 主键
     * 
     * @return array
     */
    public function find($id) {
        if (!$this->_getEnable()) {
            return null;
        }
        $data = $this->getRedis()->get($this->_calcKey($id));
        return json_decode($data, true);
    }

    /**
     * 查找key
     * 
     * @param string $key
     * @return 
     */
    public function keys($key) {
        return $this->getRedis()->keys($this->_calcKey($key));
    }

    /**
     * @param int $id 主键
     * 
     * @return array
     */
    public function set($id, $value, $timeout = 0) {
        if (!$this->_getEnable()) {
            return null;
        }
        $result = $this->getRedis()->set($this->_calcKey($id), json_encode($value));
        if ($timeout > 0) {
            $this->getRedis()->expire($this->_calcKey($id), $timeout);
        }
        return $result;
    }

    /**
     * 更新数据
     * 
     * @param int $id
     * @param array $data
     */
    public function update($id, $data, $timeout = 0) {
        if (!$this->_getEnable()) {
            return null;
        }
        $result = $this->getRedis()->set($this->_calcKey($id), json_encode($data));
        $this->getRedis()->expire($this->_calcKey($id), $timeout);
        return $result;
    }

    /**
     * 删除数据
     * 
     * @param int $id
     */
    public function remove($id) {
        return $this->getRedis()->del($this->_calcKey($id));
    }

    /**
     * 递增
     * 
     * @param type $id
     * @return type
     */
    public function incr($id) {
        return $this->getRedis()->incr($this->_calcKey($id));
    }

    /**
     * 递增
     * 
     * @param type $id
     * @return type
     */
    public function incrBy($id, $value) {

        return $this->getRedis()->incrBy($this->_calcKey($id), $value);
    }

    /**
     * 递减
     * 
     * @param type $id
     * @return type
     */
    public function decr($id) {
        return $this->getRedis()->decr($this->_calcKey($id));
    }

    /**
     * 入队列
     *
     * 详细说明
     * @形参
     * @访问      公有
     * @返回值    void
     * @throws 
     */
    public function _rpush($key, $val) {
        return $this->getRedis()->rpush($this->_calcKey($key), $val);
    }

    /**
     * 出队(左弹出)
     *
     * 详细说明
     * @形参
     * @访问      公有
     * @返回值    void
     * @throws 
     */
    public function _lpop($key) {
        return $this->getRedis()->lpop($this->_calcKey($key));
    }

    /**
     * 获取list长
     *
     * 详细说明
     * @形参
     * @访问      公有
     * @返回值    void
     * @throws 
     */
    public function _llen($key) {
        return $this->getRedis()->llen($this->_calcKey($key));
    }

    /**
     * mget方法（数组）
     * 
     * @param type $id
     * @return type
     */
    public function mget($id) {
        return $this->getRedis()->mget($id);
    }

    /**
     * 类实例
     * 
     * @var app\common\redis\db0\Userinfo
     */
    private static $_instance = null;

    /**
     * 获取类实例
     * 
     * @return app\common\redis\db0\Userinfo
     */
    public static function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

}
